// =============================================================
// CDHTKey
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Oct. 9, 2007
// =============================================================
#include "CDHTKey.h"
#include "CSHA1.h"

CDHTKey::CDHTKey()
{
}

CDHTKey::~CDHTKey()
{
}

//--------------------------------------------------------------
//			for each key's space key
//--------------------------------------------------------------
// if change lookup's behaviors, just override hash() function
//--------------------------------------------------------------
string &CDHTKey::hash(char *key, string &hashkey)
{

// because of 32/64bit compatibility, deprecated
// -----------------------------------------------------------
/*
	CMD5 md5;
	unsigned char digest[32];
	md5.GetMD5Message(key, digest);

	char tmp[256];
	hashkey = "";
	for(int i=0;i<16;i++) {
		sprintf(tmp, "%02x",digest[i]);
		hashkey += tmp;
	}
*/
// -----------------------------------------------------------

	CSHA1 sha;
	unsigned digest[5];
	sha.Reset();
	sha << key;

	if(!sha.Result(digest)) {
		cerr << "error : sha1 digest" << endl;

		hashkey = key;
	}

	char tmp[256];
	hashkey = "";
	for(int i=0;i<5;i++) {
		sprintf(tmp, "%08x", digest[i]);
		hashkey += tmp;
	}

	return hashkey;
}
//--------------------------------------------------------------

//--------------------------------------------------------------
//			for each node's space key
//--------------------------------------------------------------
// if change configure's behaviors, just override get_spacekey() function
string &CDHTKey::get_spacekey(CServiceInfo &service_info, string &spacekey)
{
	char make_key[256];
	sprintf(make_key, "%s%d%s",service_info.getIP(),service_info.getPort(),service_info.getName());

	spacekey = hash(make_key, spacekey);

	return spacekey;
}
//--------------------------------------------------------------
